var imageUtil = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nullTip:true,
    httphost: app.data.httphost,
    httpserver: app.data.httpserver,
    res:{},
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imagewidth: 0, // 缩放后的宽
    imageheight: 0, // 缩放后的高
    windowHeight: 0,
    scoreType:"",
  },
  imageLoad: function (e) {
    var imageSize = this.imageUtil(e)
    this.setData({
      imagewidth: imageSize.imageWidth,
      imageheight: imageSize.imageHeight,
      windowHeight: imageSize.windowHeight
    })
  },
  imageUtil: function (e) {
    var imageSize = {};
    var originalWidth = e.detail.width; // 图片原始宽
    var originalHeight = e.detail.height; // 图片原始高
    var originalScale = originalHeight/originalWidth; //图片高宽比
    //获取屏幕宽高
    wx.getSystemInfo({
      success: function (res) {
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      imageSize.windowHeight=windowHeight;
      var windowscale = windowHeight/windowWidth;//屏幕高宽比
      if (originalScale < windowscale) { // 图片高宽比小于屏幕高宽比
        //图片缩放后的宽为屏幕宽
        imageSize.imageWidth = windowWidth;
        imageSize.imageHeight = (windowWidth * originalHeight) / originalWidth;
      } else { // 图片高宽比大于屏幕高宽比
        //图片缩放后的高为屏幕高
        imageSize.imageHeight = windowHeight;
        imageSize.imageWidth = (windowHeight * originalWidth) / originalHeight;
      }
    }
   })
   return imageSize;
  },

  /**
   * Lgd 获取用户信息
   *  
   */
  bindGetUserInfo(res){
      console.log("----登录----"+res)
      if(res.detail.errMsg =="getUserInfo:ok"){
        
//function (options) {
  console.log("-----------------欢迎登录")
  var that = this;
  wx.login({
    success: function (r) {
      var code = r.code;//登录凭证
      console.log(code);
      if (code) {
        //2、调用获取用户信息接口
        var session_id = wx.getStorageSync('session_id');//本地取存储的sessionID  
        console.log("^^^^^^^^^^^^^^^^"+session_id+"-----"+app.data.httpserver + 'app/weixinlogin')
        //3.请求自己的服务器，解密用户信息 获取unionId等加密信息
            wx.request({
              url: app.data.httpserver + 'app/weixinlogin',//自己的服务接口地址
              data: { encryptedData: res.detail.encryptedData, iv: res.detail.iv, code: code },
              success: function (request) {
                //4.解密成功后 获取自己服务器返回的结果
                console.log(request)
                if (request.data.result == 1) {
                  var userInfo = request.data.shopUser
                  var session_id = request.data.session_id
                  var header = { 'content-type': 'application/x-www-form-urlencoded', 'Cookie': 'session_id=' + session_id }
                  var header_upload = { 'content-type': 'multipart/form-data', 'Cookie': 'session_id=' + session_id }
                  wx.setStorageSync('header', header)
                  wx.setStorageSync('header_upload', header_upload)
                  wx.setStorageSync('session_id', session_id)
                  wx.setStorageSync("userInfo", userInfo);
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                } else {
                  console.log('解密失败')
                }
              },
              fail: function () {
                console.log('系统错误')
              }
            })
          

      } else {
        console.log('获取用户登录态失败！' + r.errMsg)
        return;
      }
    },
    fail: function () {
      console.log('登陆失败')
      return;
    }
  }) 
      }else{
        wx.showModal({
          title: '警告',
          content: '您点击了拒绝授权，将无法正常使用商城功能。请10分钟后再次点击授权，或者删除小程序重新进入。',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/weclome/index'
              })
            }
            return;
          }
        })
      }
       
    },
    
  /**
   * 生命周期函数--监听页面加载
   */
onLoad: function (options) {
  var that = this;

  wx.request({
    url: that.data.httpserver + 'app/welcomeBanner?bannerType=6',
    //header: wx.getStorageSync('header'),
    success:function(resqust){
      var banner = resqust.data.bannerObj
      console.log("img*********"+banner.ban_img)
      that.setData({
        bannerPath: banner.ban_img,
      })
    }
  })
  },
  get_open(){
    wx.openSetting({
      // success: (res) => {
      //   if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
         
      //   }
      // }
    }) 
  // wx.openSetting({
  //   success: function (res) {
  //     if (!res.authSetting["scope.userInfo"] || !res.authSetting["scope.userLocation"]) {
  //       //这里是授权成功之后 填写你重新获取数据的js
  //       //参考:
  //       that.getLogiCallback('', function () {
  //         callback('')
  //       })
  //     }
  //   }
  // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})